
var xmlhttp;
var response = '';

$( addnew );


$(document).ready(function() {
	$("#divTest1").text("Hello, world!");
	$("#isstate").focusout(function(){
		var states = ['PA', "CA"] // of course you will need to add more
		var in_array = $.inArray($("#isstate").val().toUpperCase(), states);
		if (in_array == -1) {
			document.forms['CTTMTG'].elements['isstate'].focus();
		}
	});

	$( "#datepicker" ).datepicker({dateFormat:"dd/mm/yy"});


	$("input").keydown(function(){
		//alert(event.keyCode );
		
		var self = $(this)
		  , form = self.parents('form:eq(0)')
		  , focusable
		  , next
		  ;
		if (event.keyCode == 13) {
			focusable = form.find('input,a,select,button,textarea').filter(':visible');
			next = focusable.eq(focusable.index(this)+1);
			if (next.length) {
				//alert(event.keyCode );
				next.focus();
			} else {
				//form.submit();
			}
			return false;
		}
	});
	$("#mant").unbind("keydown");
	$("#matk").unbind("keydown");
	
	var validator = $("#CTTMTG").validate();
	jQuery("#isstate").blur(function() {

		var states = ['PA', "CA"] // of course you will need to add more
		var in_array = $.inArray(value.toUpperCase(), states);
		if (in_array == -1) {
			return false;
		}else{
			return true;
		}
	});

	jQuery.validator.addMethod('integer', function (value, element, param) {
        return (value != 0) && (value == parseInt(value, 10));
    }, 'Please enter a non zero integer value!');

 
});



function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
	//alert(charCode);
	if (charCode == 46 )
	{
		return true;
	}
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}    


function OnBlurInput(aData, v, elem)
{

	var a = aData.split(",");

	var i;
	var co = 0;
	for (i=0; i<a.length; i++) 
		{ if (a[i].indexOf(v) >=0) 
			{
			co = 1;
			break;
			}

		}

		
	if (co == 0) {
		//document.forms['CTTMTG'].elements['lydo'].focus();
		elem.focus();
	}

}

function addnew() {

  // Insert a DOM element object
  $('#myDiv').load('http://localhost/duoctu3/templates/acctKT/SDTTForm_add.php');
  

  var newPara = document.createElement( 'p' );
  var newParaText = document.createTextNode( "Here's a DOM element" );
  newPara.appendChild( newParaText );
  $('#myDiv').before( newPara );

  // Insert a string of HTML
  $('#myDiv').before( "<p>kkkk</p>" );

  // Insert a jQuery object
  $('#myDiv').before( $("<p>Here's a jQuery object</p>") );

  // Insert the output from a callback function
  $('#myDiv').before( sayHello );

  function sayHello() {
    return "<p>Hello! This is the output from a callback function</p>";
  }

  //onclick='$("#myTable > tbody").append("{$newrow}");'


}

 function addnewrow()
 {
	
    var x=document.getElementById('myTable');
	
    var new_row = x.rows[3].cloneNode(true);
    var len = x.rows.length;

  
    var inp1 = new_row.cells[0].getElementsByTagName('input')[0];
    inp1.id += len;
    inp1.value = '';
    var inp2 = new_row.cells[1].getElementsByTagName('input')[0];
    inp2.id += len;
    inp2.value = '';
	
    x.appendChild( new_row );
 }


function addnewrow2()
{
rowNum =1;
var row = '<p id="rowNum'+rowNum+'">Item quantity: <input type="text" name="qty[]" size="4" value="'+frm.add_qty.value+'"> Item name: <input type="text" name="name[]" value="'+frm.add_name.value+'"> <input type="button" value="Remove" onclick="removeRow('+rowNum+');"></p>';
jQuery('#myTable').append(row);
frm.add_qty.value = '';
frm.add_name.value = '';
}



function loadXMLDoc(url,cfunc)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}

function zShowMATK(str)
{
loadXMLDoc("gethint.php?q="+str,function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  });
}

function zzShowMATK(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="iiiiiiii";
  return;
  }

if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","http://localhost/duoctu3/classes/acct/js/gethint.php?q="+str,true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send();

}


function ShowMATK(q,dmtk)
{
	if (q.length==0)
	  { 
	  document.getElementById("txtHint").innerHTML="H&#227;y nh&#7853;p M&#227; TK";
	  return;
	  }

	var hint="";
	var nhap="";
	var stk = "";

	dmtk = dmtk.split(",");
	q=q.toLowerCase(); 
	len=q.length;
	var pos = -1;

		
	for (i = 0; i < dmtk.length; ++i) 
	{ 
		stk = dmtk[i].toLowerCase();
		pos = stk.indexOf(q);
		if (pos ==0)
		{
			if (hint=="")
			{
				hint = "<a onclick=\"getMATK('" + dmtk[i]+  "')"+ "\">"+dmtk[i]+"</a>";
			}
			else
			{
			hint = hint+"</br><a onclick=\"getMATK('" + dmtk[i]+  "')"+ "\">"+dmtk[i]+"</a>";
			
			}
		}
	}
	document.getElementById("txtHint").innerHTML=hint;

}

function getMATK(q)
{
	document.getElementById("matk").value=q;
	document.getElementById("txtHint").innerHTML="";

}



function ShowHint(q,aData, txtHint, txtMa)
{
//	if (q.length==0)
//	  { 
//	  document.getElementById(txtHint).innerHTML="H&#227;y nh&#7853;p M&#227; ";
//	  return false;
//	  }

	dmtk = aData.split(",");
	if (event.keyCode  ==13)
	{
		if (dmtk.indexOf(q)<0)
		{
			alert("Ban nhap chua dung ma");
			return false;
		}
		else
		{

		}
	}
	

	var hint="";
	var stk = "";

	
	q=q.toLowerCase(); 
	var pos = -1;

	if (q.length ==0)
	{
		for (i = 0; i < dmtk.length; ++i) 
		{ 
			if (hint=="")
			{
				hint = "<a onclick=\"getMA('" + dmtk[i]+  "','"+txtMa+"',"+ "'"+ txtHint+"')"+ "\">"+dmtk[i]+"</a>";
			}
			else
			{
			hint = hint+"</br><a onclick=\"getMA('" + dmtk[i]+  "','"+txtMa+"',"+ "'"+ txtHint+"')"+ "\">"+dmtk[i]+"</a>";

			}
		}
	}
	else
	{
		for (i = 0; i < dmtk.length; ++i) 
		{ 
			stk = dmtk[i].toLowerCase();
			pos = stk.indexOf(q);
			if (pos ==0)
			{
				if (hint=="")
				{
					hint = "<a onclick=\"getMA('" + dmtk[i]+  "','"+txtMa+"',"+ "'"+ txtHint+"')"+ "\">"+dmtk[i]+"</a>";
				}
				else
				{
				hint = hint+"</br><a onclick=\"getMA('" + dmtk[i]+  "','"+txtMa+"',"+ "'"+ txtHint+"')"+ "\">"+dmtk[i]+"</a>";

				}
			}
		}
	}
	document.getElementById(txtHint).innerHTML=hint;
}



function getMA(q, txtMa, txtHint)
{
	var subdata = q.split("-");
	document.getElementById(txtMa).value=subdata[0];
	document.getElementById(txtHint).innerHTML=subdata[1];;

}

function testFieldNumber(field) {
    var regExpr = new RegExp("^\d*\.?\d*$");
    if (!regExpr.test(field)) {
      // Case of error
      field.value = "";
    }
}

function UpdateRecord(host, username, password, databaseName, year, urlupdate  )
  {
	  //nhattdk
	  var matk=document.getElementById('matk').value;
	  var mant=document.getElementById('mant').value;
	  var madv = document.getElementById('madv').value;
	  var dknont = document.getElementById('dknont').value;
	  var dknovn = document.getElementById('dknovn').value;
      jQuery.ajax({
       type: "POST",
       url: urlupdate,
	   data: {host: host, username: username, password: password, databaseName: databaseName, year: year, matk:matk },
       cache: false,
       success: function(response)
       {
		
         alert("Ghi xong!");

       },
	
	   error: function(data, errorThrown)
	  {
		  alert('request failed :'+errorThrown);
	  }
     });
  }



function UpdateRecordCTTMTG(host, username, password, databaseName, year, urlupdate  )
  {
	  //nhattdk
	
		var data = Array();



		$(".mydatarow").each(function(i, v){
			data[i] = Array();
			$(this).find('input').each(function(ii, vv){
				data[i][ii] = $(this).val();
			}); 
		})

	

	  jQuery.ajax({
       type: "POST",
       url: urlupdate,
	   data: {host: host, username: username, password: password, databaseName: databaseName, tableArr: data},
       cache: false,
       success: function(response)
       {
		   alert(response);
		 alert("Ghi xong!");

       },
	
	   error: function(data, errorThrown)
	  {
		  alert('request failed :'+errorThrown);
	  }
     });
  }



